<template>
  <div class="addgoods">
    <el-form :model="brand.subsign" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="brand.subsign.name"></el-input>
      </el-form-item>

      <el-form-item label="品牌图片">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadFile01"
        >
          <img v-if="brand.subsign.pic_url" :src="brand.subsign.pic_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="品牌列表图片">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadFile02"
        >
          <img v-if="brand.subsign.list_pic_url" :src="brand.subsign.list_pic_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="APP专用图片">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadFile03"
        >
          <img
            v-if="brand.subsign.app_list_pic_url"
            :src="brand.subsign.app_list_pic_url"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="最低价" prop="name">
        <el-input v-model="brand.subsign.floor_price"></el-input>
      </el-form-item>

      <el-form-item label="新品" prop="delivery">
        <el-switch v-model="brand.subsign.is_show"></el-switch>
      </el-form-item>

      <el-form-item label="显示" prop="delivery">
        <el-switch v-model="brand.subsign.is_new"></el-switch>
      </el-form-item>

      <el-form-item label="排序" prop="name">
        <el-input v-model="brand.subsign.sort_order"></el-input>
      </el-form-item>

      <el-form-item label="品牌描述" prop="desc">
        <el-input type="textarea" v-model="brand.subsign.simple_desc"></el-input>
      </el-form-item>

      <!-- 当前位置 -->

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//引入vuex
import { mapState } from "vuex";

//引入API
import { postAddbrandInfo } from "@/request/api.js";

export default {
  methods: {
    beforeAvatarUpload() {},
    handleAvatarSuccess() {},

    // 上传单张图片1
    uploadFile01(item) {
      console.log("单张图片", item);
      let form = new FormData();
      form.append("good_pic", item.file); // 发送请求
      this.$store.dispatch("brand/categoryPutimg01", form);
    },
    // 上传单张图片2
    uploadFile02(item) {
      console.log("单张图片", item);
      let form = new FormData();
      form.append("good_pic", item.file); // 发送请求
      this.$store.dispatch("brand/categoryPutimg02", form);
    },
    // 上传单张图片3
    uploadFile03(item) {
      console.log("单张图片", item);
      let form = new FormData();
      form.append("good_pic", item.file); // 发送请求
      this.$store.dispatch("brand/categoryPutimg03", form);
    },

    submitForm() {
      //修改本地vuex
      this.$store.commit("brand/goodsGetvalue", this.brand.subsign);
      //发起请求
      this.$store.dispatch("brand/goodsAddgoods");

      console.log("查看参数", this.brand.subsign);
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    //初始化vuex
    let createds = {
      name: "",
      pic_url: "",
      list_pic_url: "",
      app_list_pic_url: "",
      floor_price: "",
      is_new: false,
      is_show: false,
      sort_order: "",
      simple_desc: ""
    };
    this.$store.commit("categor/goodscreatedsubsign", createds);

    this.$store.dispatch("categor/categorygetTop");
    console.log(this.$route.params.id);

    //判断是不是编辑内容
    if (this.$route.params.id) {
      postAddbrandInfo({
        id: this.$route.params.id
      }).then(res => {
        // console.log("获取到商品详情", res.data);
        this.$store.commit("brand/goodseditsubsign", res.data);
      });
    }
  },
  computed: {
    ...mapState(["brand"]),
    ...mapState(["categor"])
  }
};
</script>

<style lang="less" scoped>
.addgoods {
  box-sizing: border-box;
  background-color: #fff;
  padding: 10px 0 0 20px;
  height: 95%;
  overflow-y: auto;
}
.addput011 {
  font-size: 12px;
  color: #999;
}

.el-select-dropdown__item.selected {
  color: #606266;
  font-weight: normal;
}
.el-input,
.el-textarea {
  width: 220px;
}

/deep/.avatar-uploader {
  border: 1px solid #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 187px;
  height: 105px;
}

/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 187px;
  height: 105px;
  line-height: 105px;
  text-align: center;
}

/deep/.avatar {
  width: 187px;
  height: 105px;
  display: block;
}
</style>